<template>
  <ex-dialog
    width="800px"
    class="ex-diolog"
    :title="title"
    :visible="visible"
    @closeDialog="handleClose"
  >	
		<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm">
      <el-form-item label="活动名称" prop="name">
				<el-input type="text" v-model="ruleForm.name" autocomplete="off" placeholder="请填写活动名称"></el-input>
			</el-form-item>
      <el-form-item label="活动类型：" prop="type">
        <el-select v-model="ruleForm.type"  filterable placeholder="请选择活动类型">
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.value"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="下注截止时间" prop="deadline" v-if="ruleForm.type == 2">
				<el-date-picker v-model="ruleForm.deadline" type="datetime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm" >
        </el-date-picker>
			</el-form-item>
      <el-form-item label="下注资产类型：" prop="betType">
        <!-- <el-checkbox-group v-model="ruleForm.betType" @change="aaaa">
          <el-checkbox v-for="(item) in betTypeList" :label="item.id" :key="item.id">{{item.value}}</el-checkbox>
        </el-checkbox-group> -->
        <el-radio-group v-model="ruleForm.betType">
          <el-radio v-for="(item) in betTypeList" :label="item.id" :key="item.id">{{item.value}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="最低下注金额" prop="minBetAmount">
				<el-input type="text" v-model="ruleForm.minBetAmount" autocomplete="off" placeholder="填写最低下注金额"></el-input>
			</el-form-item>
      <el-form-item label="活动图片" prop="img">
          <el-upload class="avatar-uploader2" :action="action" :show-file-list="false" :on-success="imgSuccess"
            :on-error="onError" :on-progress="onProgress" :multiple="false">
            <img class="banner_img" :src="ruleForm.img" alt="" v-if="ruleForm.img">
            <i class="el-icon-plus" style="font-size: 28px" v-else v-loading="imgLoading"></i>
          </el-upload>
      </el-form-item>
			<el-form-item label="活动规则" prop="ruleImg">
          <el-upload class="avatar-uploader2" :action="action" :show-file-list="false" :on-success="ruleImgSuccess"
            :on-error="onError" :on-progress="onProgress" :multiple="false">
            <img class="banner_img" :src="ruleForm.ruleImg" alt="" v-if="ruleForm.ruleImg">
            <i class="el-icon-plus" style="font-size: 28px" v-else v-loading="imgLoading"></i>
          </el-upload>
      </el-form-item>
		
			<el-form-item>
				<el-button type="primary" @click="submitForm('ruleForm')" :loading="loading">确认</el-button>
				<el-button @click="handleClose('ruleForm')">取消</el-button>
			</el-form-item>
		</el-form>
  </ex-dialog>
</template>
<script>
// components
import Loading from '@/components/Loading/index'
// api
import {
  addGuess,
  editGuess,
  guessDetail
} from '@/api/blindBox/quiz'

export default {
  components: {
    Loading
  },
  props: {
    visible: Boolean,
    id: String,
    pathType: String,
  },
  data() {
    var validateMinBetAmount= (rule, value, callback) => {
      var reg = /(^[1-9]\d*$)/
      if (value === '') {
        callback(new Error('填写最低下注金额'));
      } else if (!reg.test(value)) {
        callback(new Error('请填写大于0的整数'));
      }  else {
        callback();
      }
    };
    return {
      loading: false,
      title: '新增竞猜活动',
      betTypeList: [
        {id: '1',value: '神椰coco'}
      ],
      typeList: [
        {id: '1',value: '支持竞猜'},
        {id: '2',value: '冠军竞猜'},
      ],
      roleList: [],
      action: process.env.VUE_APP_BASE_API + "/common/v1/upload",
      imgLoading: false,
			ruleForm: {
        betType: '',//	投注类型 1神椰coco
				deadline: '',//下注截止时间
				id: '',//竞猜活动id
        img: '',//活动图片
        minBetAmount: null,//最低下注金额
				name: '',//活动名称
        ruleImg: '',//活动规则图片
        type: '',//活动类型 1支持竞猜 2冠军竞猜
      },
      flags: false,
			rules: {
          time:   [
            { required: true,message: '请选择时间',trigger: 'change' }
          ],
          name: [
            { required: true,message: '请填写活动名称',trigger: 'blur' }
          ],
          betType: [
            { required: true,message: '请选择下注资产类型',trigger: 'change' }
          ],
          type: [
            { required: true,message: '请选择活动类型', trigger: 'change' }
          ],
          minBetAmount: [
            { required: true,validator: validateMinBetAmount,trigger: 'blur' }
          ],
          img: [
            { required: true,message: '请上传活动图片', trigger: 'change' }
          ],
          ruleImg: [
            { required: true,message: '请上传活动规则', trigger: 'change' }
          ],
          deadline: [
            { required: true,message: '请选择截止时间', trigger: 'change' }
          ],
        }
    }
  },
  computed: {
   
  },
  created() {
    if(this.pathType == 'edit'){
      this.title = '编辑竞猜活动'
      this.getDetail()
    } else if(this.pathType == 'add'){
      this.ruleForm.id = ''
    }   

	},
  methods: {
    //活动详情
    getDetail(){
      var id = this.id
      console.log('id',id)
      guessDetail(id).then(res => {
        this.ruleForm = res.result
      })
    },
    imgSuccess(res, file) { //文件上传成功时
      console.log(res.result.url)
      this.ruleForm.img = res.result.url
      this.$refs.ruleForm.validateField('img')
      this.imgLoading = false
    },
    ruleImgSuccess(res, file) { //文件上传成功时
      console.log(res.result.url)
      this.ruleForm.ruleImg = res.result.url
      this.$refs.ruleForm.validateField('ruleImg')
      this.imgLoading = false
    },
    onProgress(file) { //文件上传时
      this.imgLoading = true
    },
    onError(file) {
      this.imgLoading = false
    },
    //新建子账号
    submitForm(formName) {
      console.log('formName',formName)
      this.$refs[formName].validate((valid) => {
        console.log('valid',valid)
        if (valid) {
          this.loading = true
          if(this.pathType == 'add'){
            var data = {
              ...this.ruleForm
            }
            addGuess(data).then(res => {
              this.handleSuccess("创建成功")
              setTimeout(() => {
                this.loading = false
              }, 1000);
            }).catch(()=>{
               this.loading = false
            }).finally(() => {

            })
          }else if(this.pathType == 'edit'){
            var data = {
              ...this.ruleForm
            }
            editGuess(data).then(res => {
              this.handleSuccess("修改成功")
              setTimeout(() => {
                this.loading = false
              }, 1000);
            }).catch(()=>{
               this.loading = false
            }).finally(() => {
            })
          }
           
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    
		//取消
    handleClose(formName) {
      this.$emit('update:visible', false)
			// this.$refs[formName].resetFields();
    },
		//确认
		handleConfirm(){
			this.$emit('success', '你好')
		},
    handleSuccess(msg) {
      if (msg === '已拒绝') {
        var type = 'error'
      } else {
        var type = 'success'
      }
      this.$message({
        message: msg,
        type: type,
        duration: 1000,
        onClose: () => {
          this.handleClose()
          this.$emit('success')
        }
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.ex-diolog {
  ::v-deep .ex--dialog--footer {
    border: none !important;
		padding: 0 !important;
  }
	::v-deep .ex--dialog--main {
    padding: 10px 20px !important; 
  }
}
::v-deep .avatar-uploader2 .el-upload {
  width: 100% !important;
}

.avatar-uploader2 {
  width: 130px;
  height: 130px;
  text-align: center;
  line-height: 130px;
  background-color: #fbfdff;
  border: 1px dashed #c0ccda;
}

.banner_img {
  width: 130px;
  height: 130px;
}
.el-input{
  width: 300px;
}
</style>